/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.service.impl;

import com.minierp.bean.ReceiveBillBean;
import com.minierp.bean.ReceiveBillItemBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.AdjustBillDao;
import com.minierp.dao.BankAccountBalanceDao;
import com.minierp.dao.BankAccountDetailDao;
import com.minierp.dao.BusinessBillDao;
import com.minierp.dao.FinanceDetailDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.ReceiveBillDao;
import com.minierp.dao.ReceiveBillItemDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.err.DataChangedException;
import com.minierp.err.NotAllowException;
import com.minierp.helper.PageHelper;
import com.minierp.model.BusinessBill;
import com.minierp.model.BusinessBillExt;
import com.minierp.model.CodeRule;
import com.minierp.model.Establishment;
import com.minierp.model.FinanceDetail;
import com.minierp.model.ReceiveBill;
import com.minierp.model.ReceiveBillItem;
import com.minierp.model.SystemLog;
import com.minierp.service.ReceiveBillService;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ReceiveBillServiceImpl implements ReceiveBillService {
	public void txInsert(ReceiveBillBean form, ReceiveBillDao receiveBillDao,
			ReceiveBillItemDao receiveBillItemDao,
			SerialNumberDao serialNumberDao, IdGenerator idGenerator,
			BusinessBillDao businessBillDao, AdjustBillDao adjustBillDao,
			BankAccountBalanceDao bankAccountBalanceDao) throws Exception {
		ReceiveBill obj = form.getReceiveBill();
		obj.setCreateTime(new Date());
		obj.setId(idGenerator.getId(ReceiveBill.class.getName()));
		if (!form.getCodeRule().getGenerateRule().equals("MANUAL")) {
			obj.setBillCode(PageHelper.generateCode(serialNumberDao, form
					.getCodeRule(), "", form.getReceiveBill().getTallySetId()));

			for (ReceiveBill list = receiveBillDao.get(obj.getBillCode()); list != null; list = receiveBillDao
					.get(obj.getBillCode())) {
				obj.setBillCode(PageHelper.generateCode(serialNumberDao, form
						.getCodeRule(), "", form.getReceiveBill()
						.getTallySetId()));
			}
		}

		List arg16 = form.getItems();

		for (int updateBusinessBillCost = 0; updateBusinessBillCost < arg16
				.size(); ++updateBusinessBillCost) {
			ReceiveBillItemBean mark = (ReceiveBillItemBean) arg16
					.get(updateBusinessBillCost);
			ReceiveBillItem rbi = mark.getReceiveBillItem();
			if (!mark.getStatus().equals("DELETE")
					&& (new BigDecimal(mark.getThisCostStr()))
							.compareTo(new BigDecimal(0)) != 0) {
				List receList = receiveBillDao.getListByBusinessBill(rbi
						.getBusinessBillId());

				for (int j = 0; j < receList.size(); ++j) {
					ReceiveBill rb = (ReceiveBill) receList.get(j);
					if (rb.getExamined().equals("N")) {
						throw new NotAllowException("业务单"
								+ rbi.getBusinessBillCode() + "有未审核的收款单，收款单号为"
								+ rb.getBillCode() + "，请进入【账务-收款单作废】，用收款单号"
								+ rb.getBillCode()
								+ "查询找到这张收款单，先作废这张未审核的收款单，再重新开收款单。");
					}
				}
			}
		}

		if (obj.getExamined().equals("Y")) {
			obj.setExamSms("N");
			obj.setExamineTime(new Date());
		} else {
			obj.setExamSms("Y");
		}

		receiveBillDao.save(obj);
		boolean arg17 = false;
		if (obj.getExamined().equals("Y")) {
			arg17 = true;
		}

		this.saveItems(form, arg17, receiveBillItemDao, receiveBillDao,
				adjustBillDao, idGenerator);
		if (obj.getExamined().equals("Y")) {
			String arg18 = form.getClientName();
			if (obj.getDescription() != null
					&& !obj.getDescription().equals("")) {
				arg18 = arg18 + "," + obj.getDescription();
			}

			PageHelper.updateBankAccount(obj.getBankAccountId(),
					obj.getTallySetId(), obj.getId(), obj.getCost(),
					obj.getReceiveDate(), obj.getExamineTime(),
					obj.getBillCode(), "RECEIVE_BILL", "", "销售", "R",
					obj.getManagerName(), arg18, "BUSINESS", "SAVE_EXAM",
					false, (BankAccountDetailDao) null, bankAccountBalanceDao,
					idGenerator);
		}

		if (obj.getExamined().equals("Y")) {
			this.insertFinanceDetail(obj, receiveBillDao, idGenerator);
		}

	}

	public String txInsertApp(String bbCode, UserBean ub, CodeRule cr,
			ReceiveBillDao receiveBillDao, BusinessBillDao businessBillDao,
			BankAccountBalanceDao bankAccountBalanceDao,
			SerialNumberDao serialNumberDao, IdGenerator idGenerator)
			throws Exception {
		if (cr == null) {
			throw new NotAllowException("Code rule not found for receive bill");
		} else {
			BusinessBill b = businessBillDao.get(bbCode);
			if (b == null) {
				throw new NotAllowException("找不到单号为" + bbCode + "的业务单");
			} else {
				BusinessBillExt be = (BusinessBillExt) businessBillDao.get(
						BusinessBillExt.class, b.getId());
				if (be.getExamined().equals("N")) {
					throw new NotAllowException("业务单未审核，不能开收款单");
				} else if (be.getClosed().equals("Y")) {
					throw new NotAllowException("业务单已结清，不能开收款单");
				} else if (b.getTallySetId() != ub.getUser().getDfTsId()) {
					throw new NotAllowException("业务单帐套与当前开单帐套不一致，不能开收款单");
				} else if (ub.getUser().getDfBaId() == 0L) {
					throw new NotAllowException("没有设置默认收款账户，不能开收款单");
				} else {
					BigDecimal cost = b.getCost().add(be.getAdjustCost())
							.subtract(be.getCollectedCost());
					if (cost.compareTo(new BigDecimal(0)) == 0) {
						throw new NotAllowException("业务单已经收完款，不需要再开收款单。");
					} else {
						List receList = receiveBillDao.getListByBusinessBill(b
								.getId());

						for (int receiveBill = 0; receiveBill < receList.size(); ++receiveBill) {
							ReceiveBill est = (ReceiveBill) receList
									.get(receiveBill);
							if (est.getExamined().equals("N")) {
								throw new NotAllowException("业务单存在未审核的收款单"
										+ est.getBillCode() + "，不能再开收款单");
							}
						}

						ReceiveBill arg16 = new ReceiveBill();
						arg16.setBankAccountId(ub.getUser().getDfBaId());
						arg16.setClientId(b.getClientId());
						arg16.setCost(cost);
						arg16.setCreaterId(ub.getStaffer().getId());
						arg16.setCreaterName(ub.getStaffer().getStafferName());
						arg16.setCreateTime(new Date());
						arg16.setManagerId(ub.getStaffer().getId());
						arg16.setManagerName(ub.getStaffer().getStafferName());
						arg16.setDepartmentId(ub.getStaffer().getDepartmentId());
						arg16.setExaminerId(ub.getStaffer().getId());
						arg16.setExaminerName(ub.getStaffer().getStafferName());
						arg16.setExamined("Y");
						arg16.setExamSms("N");
						arg16.setExamineTime(new Date());
						arg16.setTallySetId(ub.getUser().getDfTsId());
						Establishment arg17 = (Establishment) receiveBillDao
								.get(Establishment.class, b.getClientId());
						if (arg17 != null && arg17.getManagerId() != 0L) {
							arg16.setManagerId(arg17.getManagerId());
							arg16.setManagerName(arg17.getManagerName());
						}

						arg16.setId(idGenerator.getId(ReceiveBill.class
								.getName()));
						if (cr.getGenerateRule().equals("MANUAL")) {
							throw new NotAllowException("收款单编码规则必须设置为自动产生！");
						} else {
							arg16.setBillCode(PageHelper.generateCode(
									serialNumberDao, cr, "",
									arg16.getTallySetId()));

							for (ReceiveBill c = receiveBillDao.get(arg16
									.getBillCode()); c != null; c = receiveBillDao
									.get(arg16.getBillCode())) {
								arg16.setBillCode(PageHelper.generateCode(
										serialNumberDao, cr, "",
										arg16.getTallySetId()));
							}

							receiveBillDao.save(arg16);
							ReceiveBillItem arg18 = new ReceiveBillItem();
							arg18.setBillId(arg16.getId());
							arg18.setId(idGenerator.getId(ReceiveBillItem.class
									.getName()));
							arg18.setBusinessBillCode(b.getBillCode());
							arg18.setBusinessBillId(b.getId());
							arg18.setContent(b.getContent());
							arg18.setLinkman(b.getLinkman());
							arg18.setOrderDate(b.getOrderDate());
							arg18.setRemainCost(cost);
							arg18.setTotalCost(b.getCost());
							arg18.setThisCost(cost);
							receiveBillDao.save(arg18);
							be.setHasReceive("Y");
							be.setReceiveCost(be.getReceiveCost().add(
									arg18.getThisCost()));
							be.setCollectedCost(be.getCollectedCost().add(
									arg18.getThisCost()));
							if (b.getCost().subtract(be.getCollectedCost())
									.add(be.getAdjustCost())
									.compareTo(new BigDecimal(0)) == 0) {
								be.setCollected("Y");
							} else {
								be.setCollected("N");
							}

							if (be.getReceTime() == null
									|| be.getReceTime().before(
											arg16.getReceiveDate())) {
								be.setReceTime(arg16.getReceiveDate());
							}

							receiveBillDao.update(be);
							String mark = "";
							if (arg17 != null) {
								mark = arg17.getEstName();
							}

							PageHelper.updateBankAccount(
									arg16.getBankAccountId(),
									arg16.getTallySetId(), arg16.getId(),
									arg16.getCost(), arg16.getReceiveDate(),
									arg16.getExamineTime(),
									arg16.getBillCode(), "RECEIVE_BILL", "",
									"销售", "R", arg16.getManagerName(), mark,
									"BUSINESS", "SAVE_EXAM", false,
									(BankAccountDetailDao) null,
									bankAccountBalanceDao, idGenerator);
							this.insertFinanceDetail(arg16, receiveBillDao,
									idGenerator);
							return arg16.getBillCode();
						}
					}
				}
			}
		}
	}

	private void insertFinanceDetail(ReceiveBill obj,
			ReceiveBillDao receiveBillDao, IdGenerator idGenerator) {
		FinanceDetail fd = new FinanceDetail();
		fd.setId(idGenerator.getId(FinanceDetail.class.getName()));
		fd.setBillDate(obj.getReceiveDate());
		fd.setBillId(obj.getId());
		fd.setBillType("RECEIVE_BILL");
		fd.setRemCost((new BigDecimal(0)).subtract(obj.getCost()));
		fd.setRpCost(obj.getCost());
		fd.setEstId(obj.getClientId());
		fd.setFinanceType("R");
		fd.setTallySetId(obj.getTallySetId());
		receiveBillDao.save(fd);
	}

	public void txUpdate(ReceiveBillBean form, ReceiveBillDao receiveBillDao,
			ReceiveBillItemDao receiveBillItemDao, IdGenerator idGenerator,
			BusinessBillDao businessBillDao, AdjustBillDao adjustBillDao,
			BankAccountBalanceDao bankAccountBalanceDao) throws Exception {
		ReceiveBill obj = form.getReceiveBill();
		Object[] sv = receiveBillDao.getStatusValues(obj.getId());
		if (sv == null) {
			throw new Exception("Can\'t get status values for receive bill:"
					+ obj.getId());
		} else {
			long existSycVal = ((Date) sv[0]).getTime();
			String exitEnabled = sv[1].toString();
			String existExamined = sv[2].toString();
			if (existSycVal != form.getSychronizeValue()) {
				throw new DataChangedException("收款单已被其他操作修改，请关闭页面再打开后重新操作！");
			} else if (exitEnabled.equals("N")) {
				throw new NotAllowException("收款单已作废，不能修改！");
			} else if (existExamined.equals("Y")) {
				throw new NotAllowException("收款单已审核，不能修改！");
			} else {
				if (obj.getExamined().equals("Y")) {
					obj.setExamSms("N");
					obj.setExamineTime(new Date());
				} else {
					obj.setExamSms("Y");
				}

				obj.setUpdateTime(new Date());
				receiveBillDao.update(obj);
				boolean updateBusinessBillCost = false;
				if (obj.getExamined().equals("Y")) {
					updateBusinessBillCost = true;
				}

				this.saveItems(form, updateBusinessBillCost,
						receiveBillItemDao, receiveBillDao, adjustBillDao,
						idGenerator);
				if (obj.getExamined().equals("Y")) {
					String mark = form.getClientName();
					if (obj.getDescription() != null
							&& !obj.getDescription().equals("")) {
						mark = mark + "," + obj.getDescription();
					}

					PageHelper.updateBankAccount(obj.getBankAccountId(),
							obj.getTallySetId(), obj.getId(), obj.getCost(),
							obj.getReceiveDate(), obj.getExamineTime(),
							obj.getBillCode(), "RECEIVE_BILL", "", "销售", "R",
							obj.getManagerName(), mark, "BUSINESS",
							"SAVE_EXAM", false, (BankAccountDetailDao) null,
							bankAccountBalanceDao, idGenerator);
				}

				if (obj.getExamined().equals("Y")) {
					this.insertFinanceDetail(obj, receiveBillDao, idGenerator);
				}

			}
		}
	}

	private void saveItems(ReceiveBillBean form,
			boolean updateBusinessBillCost,
			ReceiveBillItemDao receiveBillItemDao,
			ReceiveBillDao receiveBillDao, AdjustBillDao adjustBillDao,
			IdGenerator idGenerator) throws NotAllowException {
		List items = form.getItems();
		int newNum = 0;

		for (int curItemId = 0; curItemId < items.size(); ++curItemId) {
			ReceiveBillItemBean bean = (ReceiveBillItemBean) items
					.get(curItemId);
			if (bean.getStatus().equals("NEW")
					&& bean.getReceiveBillItem().getThisCost()
							.compareTo(new BigDecimal(0)) != 0) {
				++newNum;
			}
		}

		long arg18 = 0L;
		if (newNum > 0) {
			arg18 = idGenerator.getId(ReceiveBillItem.class.getName(), newNum);
		}

		for (int i = 0; i < items.size(); ++i) {
			ReceiveBillItemBean bean1 = (ReceiveBillItemBean) items.get(i);
			if (bean1.getOldId() != 0L || !bean1.getStatus().equals("DELETE")) {
				BusinessBill b = (BusinessBill) receiveBillDao.get(
						BusinessBill.class, bean1.getReceiveBillItem()
								.getBusinessBillId());
				BusinessBillExt be = (BusinessBillExt) receiveBillDao.get(
						BusinessBillExt.class, bean1.getReceiveBillItem()
								.getBusinessBillId());
				if (b == null) {
					throw new NotAllowException("找不到id为"
							+ bean1.getReceiveBillItem().getBusinessBillId()
							+ "的业务单");
				}

				bean1.getReceiveBillItem().setBillId(
						form.getReceiveBill().getId());
				String doDb = "N";
				String doDel = "N";
				String doIns = "N";
				if (bean1.getStatus().equals("DELETE")) {
					if (bean1.getOldId() != 0L) {
						receiveBillItemDao.delete(bean1.getOldId());
						doDb = "Y";
						doDel = "Y";
					}
				} else if (bean1.getStatus().equals("EDIT")) {
					if (bean1.getReceiveBillItem().getThisCost()
							.compareTo(new BigDecimal(0)) != 0) {
						receiveBillItemDao.update(bean1.getReceiveBillItem());
						doDb = "Y";
					} else {
						receiveBillItemDao.delete(bean1.getOldId());
						doDb = "Y";
						doDel = "Y";
					}
				} else if (bean1.getStatus().equals("NEW")) {
					if (bean1.getReceiveBillItem().getThisCost()
							.compareTo(new BigDecimal(0)) != 0) {
						bean1.getReceiveBillItem().setId(arg18);
						receiveBillItemDao.save(bean1.getReceiveBillItem());
						++arg18;
						doDb = "Y";
						doIns = "Y";
					}
				} else if (bean1.getStatus().equals("UNCHANGE")) {
					doDb = "Y";
				}

				if (doDb.equals("Y")) {
					if (be.getExamined().equals("N")) {
						throw new NotAllowException("单号为" + b.getBillCode()
								+ "的业务单还没审核，不能操作");
					}

					if (be.getEnabled().equals("N")) {
						throw new NotAllowException("单号为" + b.getBillCode()
								+ "的业务单已作废，不能操作");
					}

					if (be.getClosed().equals("Y")) {
						throw new NotAllowException("单号为" + b.getBillCode()
								+ "的业务单已结清，不能操作");
					}

					if (b.getClientId() != form.getReceiveBill().getClientId()) {
						throw new NotAllowException("不同客户的业务单不能一起操作，单号："
								+ b.getBillCode());
					}

					if (b.getTallySetId() != form.getReceiveBill()
							.getTallySetId()) {
						throw new NotAllowException("业务单帐套与当前开单帐套不一致，单号："
								+ b.getBillCode());
					}
				}

				if (doDel.equals("Y")) {
					List iList = receiveBillItemDao.getListByBusinessBill(b
							.getId());
					if (iList.size() > 0) {
						be.setHasReceive("Y");
					} else {
						be.setHasReceive("N");
					}
				} else if (doIns.equals("Y")) {
					be.setHasReceive("Y");
				}

				if (updateBusinessBillCost && doDb.equals("Y")
						&& doDel.equals("N")) {
					be.setReceiveCost(be.getReceiveCost().add(
							bean1.getReceiveBillItem().getThisCost()));
					be.setCollectedCost(be.getCollectedCost().add(
							bean1.getReceiveBillItem().getThisCost()));
					if (b.getCost().subtract(be.getCollectedCost())
							.add(be.getAdjustCost())
							.compareTo(new BigDecimal(0)) == 0) {
						be.setCollected("Y");
					} else {
						be.setCollected("N");
					}

					if (be.getReceTime() == null
							|| be.getReceTime().before(
									form.getReceiveBill().getReceiveDate())) {
						be.setReceTime(form.getReceiveBill().getReceiveDate());
					}
				}

				receiveBillDao.update(be);
			}
		}

	}

	public void txExamine(long id, ReceiveBillDao receiveBillDao,
			ReceiveBillItemDao receiveBillItemDao,
			BusinessBillDao businessBillDao, AdjustBillDao adjustBillDao,
			long examinerId, String examinerName,
			BankAccountBalanceDao bankAccountBalanceDao, IdGenerator idGenerator)
			throws Exception {
		ReceiveBill a = (ReceiveBill) receiveBillDao.get(ReceiveBill.class, id);
		if (a != null && a.getEnabled().equals("Y")
				&& a.getExamined().equals("N")) {
			List itemList = receiveBillItemDao.getListByReceiveBill(a.getId());
			a.setExamined("Y");
			a.setExamSms("N");
			a.setExamineTime(new Date());
			a.setExaminerId(examinerId);
			a.setExaminerName(examinerName);
			a.setUpdateTime(new Date());
			receiveBillDao.update(a);
			String clientName = "";
			Establishment est = (Establishment) bankAccountBalanceDao.get(
					Establishment.class, a.getClientId());
			if (est != null) {
				clientName = est.getEstName();
				if (a.getDescription() != null
						&& !a.getDescription().equals("")) {
					clientName = clientName + "," + a.getDescription();
				}
			}

			this.updateBusinessBill(a, itemList, receiveBillDao,
					businessBillDao, adjustBillDao, receiveBillItemDao, "EXA",
					"ADD", true);
			PageHelper.updateBankAccount(a.getBankAccountId(),
					a.getTallySetId(), a.getId(), a.getCost(),
					a.getReceiveDate(), a.getExamineTime(), a.getBillCode(),
					"RECEIVE_BILL", "", "销售", "R", a.getManagerName(),
					clientName, "BUSINESS", "EXAM", false,
					(BankAccountDetailDao) null, bankAccountBalanceDao,
					idGenerator);
			this.insertFinanceDetail(a, receiveBillDao, idGenerator);
		}

	}

	private void updateBusinessBill(ReceiveBill bill, List itemList,
			ReceiveBillDao receiveBillDao, BusinessBillDao businessBillDao,
			AdjustBillDao adjustBillDao, ReceiveBillItemDao receiveBillItemDao,
			String updType, String costType, boolean checkBbStatus)
			throws NotAllowException {
		for (int i = 0; i < itemList.size(); ++i) {
			ReceiveBillItem item = (ReceiveBillItem) itemList.get(i);
			BusinessBill b = (BusinessBill) receiveBillDao.get(
					BusinessBill.class, item.getBusinessBillId());
			BusinessBillExt be = (BusinessBillExt) businessBillDao.get(
					BusinessBillExt.class, item.getBusinessBillId());
			if (b == null) {
				throw new NotAllowException("找不到id为" + item.getBusinessBillId()
						+ "的业务单");
			}

			if (checkBbStatus) {
				if (be.getExamined().equals("N")) {
					throw new NotAllowException("单号为" + b.getBillCode()
							+ "的业务单还没审核，不能操作");
				}

				if (be.getEnabled().equals("N")) {
					throw new NotAllowException("单号为" + b.getBillCode()
							+ "的业务单已作废，不能操作");
				}

				if (be.getClosed().equals("Y")) {
					throw new NotAllowException("单号为" + b.getBillCode()
							+ "的业务单已结清，不能操作");
				}
			}

			if (updType.equals("EXA")) {
				if (be.getReceTime() == null
						|| be.getReceTime().before(bill.getReceiveDate())) {
					be.setReceTime(bill.getReceiveDate());
				}
			} else if (updType.equals("CAN")) {
				List iList = receiveBillItemDao
						.getListByBusinessBill(b.getId());
				if (iList.size() > 0) {
					be.setHasReceive("Y");
				} else {
					be.setHasReceive("N");
				}
			}

			if (!costType.equals("NONE")) {
				if (costType.equals("ADD")) {
					be.setReceiveCost(be.getReceiveCost().add(
							item.getThisCost()));
					be.setCollectedCost(be.getCollectedCost().add(
							item.getThisCost()));
				} else {
					be.setReceiveCost(be.getReceiveCost().subtract(
							item.getThisCost()));
					be.setCollectedCost(be.getCollectedCost().subtract(
							item.getThisCost()));
				}

				if (b.getCost().subtract(be.getCollectedCost())
						.add(be.getAdjustCost()).compareTo(new BigDecimal(0)) == 0) {
					be.setCollected("Y");
				} else {
					be.setCollected("N");
				}
			}

			receiveBillDao.update(be);
		}

	}

	public void txUnExamine(long id, ReceiveBillDao receiveBillDao,
			ReceiveBillItemDao receiveBillItemDao,
			BusinessBillDao businessBillDao, AdjustBillDao adjustBillDao,
			BankAccountBalanceDao bankAccountBalanceDao,
			BankAccountDetailDao bankAccountDetailDao,
			FinanceDetailDao financeDetailDao, IdGenerator idGenerator,
			UserBean ub) throws Exception {
		ReceiveBill a = (ReceiveBill) receiveBillDao.get(ReceiveBill.class, id);
		if (a != null && a.getEnabled().equals("Y")
				&& a.getExamined().equals("Y")) {
			a.setExamined("N");
			a.setExamSms("Y");
			a.setExaminerId(0L);
			a.setExaminerName("");
			a.setUpdateTime(new Date());
			receiveBillDao.update(a);
			List itemList = receiveBillItemDao.getListByReceiveBill(a.getId());
			this.updateBusinessBill(a, itemList, receiveBillDao,
					businessBillDao, adjustBillDao, receiveBillItemDao,
					"UN_EXA", "SUB", true);
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String mark = ub.getStaffer().getStafferName() + "于"
					+ sf.format(new Date()) + "反审核单据";
			PageHelper.updateBankAccount(a.getBankAccountId(),
					a.getTallySetId(), a.getId(),
					(new BigDecimal(0)).subtract(a.getCost()),
					a.getReceiveDate(), new Date(), a.getBillCode(),
					"RECEIVE_BILL", "", "销售", "R", a.getManagerName(), mark,
					"BUSINESS", "UN_EXAM", true, bankAccountDetailDao,
					bankAccountBalanceDao, idGenerator);
			financeDetailDao.delete(a.getId(), "RECEIVE_BILL");
		}

	}

	public void txCancel(long id, String reason, ReceiveBillDao receiveBillDao,
			ReceiveBillItemDao receiveBillItemDao,
			BusinessBillDao businessBillDao, AdjustBillDao adjustBillDao,
			UserBean ub, BankAccountBalanceDao bankAccountBalanceDao,
			BankAccountDetailDao bankAccountDetailDao,
			FinanceDetailDao financeDetailDao, IdGenerator idGenerator)
			throws Exception {
		ReceiveBill d = (ReceiveBill) receiveBillDao.get(ReceiveBill.class, id);
		if (d != null && d.getEnabled().equals("Y")) {
			d.setEnabled("N");
			d.setExamSms("N");
			d.setDisableManId(ub.getUser().getStafferId());
			d.setUpdateTime(new Date());
			receiveBillDao.update(d);
			List itemList = receiveBillItemDao.getListByReceiveBill(d.getId());
			boolean checkBbStatus = false;
			String costType = "NONE";
			if (d.getExamined().equals("Y")) {
				checkBbStatus = true;
				costType = "SUB";
			}

			this.updateBusinessBill(d, itemList, receiveBillDao,
					businessBillDao, adjustBillDao, receiveBillItemDao, "CAN",
					costType, checkBbStatus);
			if (d.getExamined().equals("Y")) {
				SimpleDateFormat log = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String mark = ub.getStaffer().getStafferName() + "于"
						+ log.format(new Date()) + "作废单据";
				PageHelper.updateBankAccount(d.getBankAccountId(),
						d.getTallySetId(), d.getId(),
						(new BigDecimal(0)).subtract(d.getCost()),
						d.getReceiveDate(), new Date(), d.getBillCode(),
						"RECEIVE_BILL", "", "销售", "R", d.getManagerName(),
						mark, "BUSINESS", "CANC", true, bankAccountDetailDao,
						bankAccountBalanceDao, idGenerator);
			}

			if (d.getExamined().equals("Y")) {
				financeDetailDao.delete(d.getId(), "RECEIVE_BILL");
			}

			SystemLog log1 = new SystemLog();
			log1.setId(idGenerator.getId(SystemLog.class.getName()));
			log1.setActionCode("RECEIVE_BILL");
			log1.setLogType("CANCEL_LOG");
			log1.setCreaterId(ub.getStaffer().getId());
			log1.setCreaterName(ub.getStaffer().getStafferName());
			log1.setDepName(ub.getDepName());
			log1.setDepId(ub.getStaffer().getDepartmentId());
			log1.setStatus("S");
			log1.setUserName(ub.getUser().getUserName());
			log1.setIp(d.getBillCode());
			log1.setMark("");
			log1.setRelId(id);
			log1.setCancelReason(reason);
			log1.setTallySetId(d.getTallySetId());
			receiveBillDao.save(log1);
		}

	}
}